<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    //寻物
    $lost = M('lost');
    $this->lostnum=$lost->count();//寻物启示的总数
    $this->lostlist=$lost->where('isfinish=0')->limit(4)->order('isfirst desc,id desc')->select();
    //招领
    $find = M('find');
    $this->findnum=$find->count();//招领启示的总数
    $this->findlist=$find->where('isfinish=0')->limit(4)->order('id desc')->select();

    $this->all=$lost->where('isfinish=1')->count()+$find->where('isfinish=1')->count();//总数的统计
//echo $lost->getLastSQL;//本方法用于显示该表最后操作的SQL语句
//var_dump
        $this->display();
    }
   // 
    public function testimg(){
        $image = new \Think\Image();
        $image->open('./Public/1.jpg');
        var_dump($image);
        $image->crop(400,400)->save('./Public/crop.jpg');
    }

   public function register(){
        $code=I('post.verifycode');
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        if($_FILES['file']['error']==0 ){
            $info=$this->upload();
            $name=$info["file"]["savename"];
        }
        else{
            $name="default.jpg";
        }
        
        $user=D('users');
        if($user->create()){
            $user->photo=$name;
            if(false!==$user->add()){
                $this->success('注册成功!','login');
            }else{$this->error('数据错误');}
        }else{
            $this->error($user->getError());
        }
    }
public function checkLogin(){
        $code=I('post.verifycode');
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        $data['telephone']=trim(I('post.telephone'));
        $data['password']=md5(I('post.password'));
        $usermodel=M('users');
        $rs=$usermodel->where($data)->select();
        if($rs){
            session("tele",$data['telephone']);
            session("alias",$rs['0']["aliasname"]);
            $this->success("登录成功",U("Goods/goods"));
        }
        else{
            $this->error("用户名或密码错误");
        }
    }

    //

public function checkLogin2(){
        $code=I('post.verifycode');
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        session("admin_name",null);
        $data['admin_name']=trim(I('post.admin_name'));
        $data['password']=I('post.password');
        $usermodel=M('admin');
        $rs=$usermodel->where($data)->select();
        if($rs){
            session("admin_name",$data['admin_name']);
            $this->success("登录成功",U("Admin/User/userlist"));
        }
        else{
            $this->error("管理员名或密码错误");
        }
    }    public function verify(){
        $Verify = new \Think\Verify();
        $Verify->entry();
    }
    public function check_verify($code,$id=''){
        $Verify = new \Think\Verify();
        return $Verify->check($code,$id);
    }   


public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/Uploads/photo/'; // 设置附件上传根目录  //  
        $upload->autoSub = false;//关闭子目录，默认true
        //$upload->savePath  =     ''; // 设置附件上传(子)目录，默认目录名为年月日
        $info   =   $upload->upload();  // 上传文件 
        if(!$info) {
            $this->error($upload->getError());    
        }
        else{
            return $info;
        }                    
    }
            public function typography(){
        //调用模型层获取数据
        $lostmodel = M('lost');
        //var_dump($lostmodel);
        $lost = $lostmodel->select();
        //desc倒序 asc顺序
        
        //调用视图层显示数据
        $this->lost=$lost;
        //var_dump($lost);
        $this->display();
    } 

}
?>